package com.dfrz.household.controller;

import com.dfrz.household.bean.User;
import com.dfrz.household.service.IUserService;
import com.dfrz.household.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@RestController
public class RegisterController {
    @Autowired
    IUserService userService;
    @RequestMapping("/toregister")
    public ModelAndView toRegister(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("register");
        return mv;
    }
    @RequestMapping("/register")
    public Result user_register(User user){
        Result result=new Result();
        User user1=userService.queryOneByLoginName(user.getUname());
        if(user1!=null){
            result.setCode(-1);
            result.setMsg("用户名已存在");
            return result;
        }
        int count =userService.save(user);
        if (count>0){
            result.setMsg("注册成功");
            result.setCode(0);
        }else{
            result.setMsg("注册失败");
            result.setCode(1);
        }
        return result;
    }
    //头像上传
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file){
        System.out.println("正在执行文件上传-------------->");
        Result result = new Result();
        //文件名
        String filename=file.getOriginalFilename();
        //上传路径
        String filePath="D:\\household_userpic";
        File destFile= new File(filePath+File.separator+filename);
        System.out.println("文件："+filePath+File.separator+filename);
        try {
            //SpringMVC实现文件IO输入输出
            file.transferTo(destFile);
            result.setCode(0);
            result.setMsg("上传成功");
            Map<String,String> map=new HashMap<String,String>();
            map.put("filename",filename);
            map.put("src",filePath+File.separator+filename);
            result.setData(map);
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(1);
            result.setMsg("上传失败");
        }

        return result;
    }
    /**
     * 下载头像
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/downheader")
    public void downpic(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始下载>>>>>>");
        //String fileName = "qq.docx";// 文件名
        String fileName=request.getParameter("fileName"); //设置文件路径
        if (fileName != null) {

            File file = new File("D:\\household_userpic\\"+fileName);
            //File file = new File(realPath , fileName);
            if (file.exists()) {
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally { // 做关闭操作
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
